﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Webdiyer.WebControls.Mvc;
using System.ComponentModel;
using xTeam.HengLe.DataAccess;
using xTeam.HengLe.Entity;

namespace xTeam.HengLe.Controller.Website
{
    public class NewsController : BaseController
    {
        public NewsController()
        {
            // 数据连接字符串
            NewsDataAccess.Con = _connectionString;
        }

        /// <summary>
        /// 新闻主页
        /// </summary>
        /// <param name="NewsId">分页码</param>
        /// <param name="classid">分类编号</param>
        /// <returns>所有新闻列表</returns>
        public ActionResult Index(int? NewsId, int? classid)
        {
            classid = classid ?? 0;
            PagedList<ENews> newsPageList = null;

            // 分类编号大于0
            if (classid > 0)
            {
                // 此分类的新闻
                newsPageList = new PagedList<ENews>(NewsDataAccess.GetByCondition(-1, "", (int)classid), NewsId ?? 1, 10);
                int newscount = newsPageList.Count;

                // 新闻个数 = 1且分类编号=查询的分类编号，直接转到新闻详细页面
                if (newscount == 1 && classid == newsPageList[0].ClassifyId)
                {
                    return RedirectToAction("NewsInfo", new { newsId = newsPageList[0].NewsId });
                }

                // 转到分类新闻列表页面
                return RedirectToAction("List", new { classid = classid });
            }

            // 所有新闻，分页
            newsPageList = new PagedList<ENews>(NewsDataAccess.GetByCondition(0, "", 0), NewsId ?? 1, 10);
            return View(newsPageList);
        }

        /// <summary>
        /// 分类新闻
        /// </summary>
        /// <param name="classid">分类编号</param>
        /// <param name="NewsId">分页码</param>
        /// <returns>新闻列表</returns>
        public ActionResult List([DefaultValue(0)]int classid, int? NewsId)
        {
            EClassify classify = _classifyDataAccess.GetClassifyById(classid);
            // 获取分类名称
            ViewData["classname"] = classify != null ? _classifyDataAccess.GetClassifyById(classid).Title : "新闻列表";
            // 分类新闻列表
            List<ENews> newsList = NewsDataAccess.GetByCondition(-1, "", classid);
            // 分页
            PagedList<ENews> newsPageList = new PagedList<ENews>(newsList, NewsId ?? 1, 10);
            return View(string.IsNullOrEmpty(classify.TemplateName) ? string.Empty : string.Format("{0}list", classify.TemplateName), newsPageList);
        }

        /// <summary>
        /// 新闻详细信息
        /// </summary>
        /// <param name="newsId">新闻编号</param>
        /// <returns>新闻详细信息</returns>
        public ActionResult NewsInfo([DefaultValue(0)]int newsId)
        {
            ENews news = null;
            string templateName = string.Empty;

            if (newsId > 0)
            {
                // >0时，根据新闻编号获取新闻
                List<ENews> newsList = NewsDataAccess.GetByCondition(newsId, "", 0);

                if (newsList.Count > 0)
                {
                    news = newsList[0];
                    templateName = _classifyDataAccess.GetClassifyById(news.ClassifyId).TemplateName;
                }
            }

            return View(templateName, news);
        }
    }
}
